/*
 * File Name：HttpHelper.java
 * Copyright：Copyright 2008-2014 CiWong.Inc. All Rights Reserved.
 * Description： HttpHelper.java
 * Modify By：Administrator
 * Modify Date：2014年11月7日
 * Modify Type：Add
 */
package com.ciwong.libs.utils.volley;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.zip.GZIPInputStream;

import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.http.protocol.HTTP;

import com.ciwong.libs.utils.volley.BaseRequest.VerifyInfo;

/**
 * TODO Description of class。
 * <p>
 * TODO Detail Description
 * <p>
 * TODO Sample Code
 * 
 * <pre>
 * </pre>
 * 
 * @author Administrator
 * @version ciwong v.1.0 2014年11月7日
 * @since ciwong v.1.0
 */
public class HttpHelper
{
    /**
     * 
     * 是否包含GZIP压缩
     * 
     * @param headers
     *            响应头
     * @return 是否包含GZIP压缩
     */
    public static boolean isGZIP(Map<String, String> headers)
    {
        String contentEncoding = headers.get(HTTP.CONTENT_ENCODING);
        return contentEncoding != null
                && contentEncoding.equalsIgnoreCase("GZIP") ? true : false;
    }

    /**
     * 
     * 解压GZIP压缩文件
     * 
     * @param data
     *            GZIP压缩后byte数组
     * @return GZIP解压后byte数组
     */
    public static byte[] unGZIP(byte[] data)
    {
        byte[] b = null;
        try
        {
            ByteArrayInputStream is = new ByteArrayInputStream(data);
            GZIPInputStream gis = new GZIPInputStream(is);

            int count;
            byte[] bytes = new byte[1024];
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            while ((count = gis.read(bytes, 0, data.length)) != -1)
            {
                baos.write(bytes, 0, count);
            }
            b = baos.toByteArray();
            baos.flush();
            baos.close();
            gis.close();
            is.close();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return b;
    }

    /**
     * 拼接请求地址
     * 
     * @param params
     *            请求参数
     * @param url
     *            请求地址
     * @return 拼接后的请求地址
     */
    public static String concatUrl(Map<String, String> params, String url)
    {
        if (params != null && !params.isEmpty())
        {
            StringBuilder sb = new StringBuilder();
            int index = 0;
            for (Entry<String, String> entry : params.entrySet())
            {
                if (index != 0 || !url.endsWith("?"))
                {
                    sb.append("&");
                }
                sb.append(entry.getKey());
                sb.append("=");
                sb.append(entry.getValue());
                index++;
            }
            return url + sb.toString();
        }
        return url;
    }

    /**
    *
    * 获取验证信息字符串
    *
    * @param verifyInfo
    *            验证信息实体类
    * @return 验证信息字符串
    */
   public static String getVerifyStr(VerifyInfo verifyInfo)
   {
       if (verifyInfo != null)
       {
           return "?clientId=" + verifyInfo.getClientId() + "&accessToken="
                   + verifyInfo.getAccessToken()+"&client_id="+verifyInfo.getClientId();
       }
       return "";
   }

    
    
    
    
    /**
     * 
     * 初始化GET请求
     * 
     * @param params
     *            请求参数
     * @param url
     *            请求地址
     * @param headers
     *            请求头
     * @return GetMethod
     * @throws Exception
     *             Exception
     */
    public static GetMethod getGetConnection(Map<String, String> params,
            String url, Map<String, String> headers) throws Exception
    {
        StringBuilder sb = new StringBuilder();
        sb.append(concatUrl(params, url));
        String connUrl = null;
        GetMethod mGetMethod = null;
        connUrl = sb.toString();
        if (connUrl != null)
        {
            mGetMethod = new GetMethod(connUrl);
            initRequest(mGetMethod, headers);
        }
        return mGetMethod;
    }

    /**
     * 初始化http请求
     * 
     * @param request
     *            HttpURLConnection
     * @param headers
     *            请求头
     */
    public static void initRequest(HttpMethod request,
            Map<String, String> headers)
    {
        request.addRequestHeader("Content-Type",
                "application/x-www-form-urlencoded; charset=utf-8");
        request.setRequestHeader("Charset", "UTF-8");
        StringBuffer sb = new StringBuffer();
        sb.append("Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.2; Trident/4.0; .NET CLR 1.1.4322; ");
        sb.append(".NET CLR 2.0.50727; .NET CLR 3.0.04506.30; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)");
        request.addRequestHeader("User-Agent", sb.toString());
        request.setRequestHeader("Accept-Encoding", "GZIP, deflate");
        if (headers != null)
        {
            Set<String> keySet = headers.keySet();
            for (String key : keySet)
            {
                request.setRequestHeader(key, headers.get(key));
            }
        }
    }
}
